package ui.activity;

import android.app.DatePickerDialog;
import android.content.Intent;
import android.os.Build;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.fy.fyzf.R;
import com.fy.fyzf.base.BaseActivity;
import com.fy.fyzf.bean.AddSignOne;
import com.fy.fyzf.bean.HousesListBean;
import com.fy.fyzf.bean.PassengerNumberBean;
import com.fy.fyzf.bean.BaseData;
import com.fy.fyzf.bean.SaveBean;
import com.fy.fyzf.bean.SignDetailBean;
import com.fy.fyzf.dialog.LoopViewPopwindow;
import com.fy.fyzf.presenter.AddSignPresenter;
import com.fy.fyzf.utils.AppUtils;
import com.fy.fyzf.utils.MathUtils;
import com.fy.fyzf.utils.SPUtils;
import com.fy.fyzf.view.AddSignCallback;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import ui.adapter.CheckBoxAdapter;

public class AddSignActivityOne extends BaseActivity<AddSignPresenter> implements AddSignCallback {

    @BindView(R.id.rb2)
    RadioButton rb2;
    @BindView(R.id.rb3)
    RadioButton rb3;
    @BindView(R.id.rg)
    RadioGroup rg;
    @BindView(R.id.tv_date)
    TextView tvDate;
    @BindView(R.id.tv_number_passenger)
    TextView tvNumberPassenger;
    @BindView(R.id.tv_number_housing)
    TextView tvNumberHousing;
    @BindView(R.id.tv_search)
    TextView tvSearch;
    @BindView(R.id.et_counselor)
    EditText etCounselor;
    @BindView(R.id.tv_conpany)
    TextView tvConpany;
    @BindView(R.id.tv_staff_name)
    TextView tvStaffName;
    @BindView(R.id.recycleView1)
    RecyclerView recycleView1;
    @BindView(R.id.recycleView2)
    RecyclerView recycleView2;
    @BindView(R.id.et_area)
    EditText etArea;
    @BindView(R.id.tv_ok)
    TextView tvOk;
    private CheckBoxAdapter checkBoxAdapter1;
    private CheckBoxAdapter checkBoxAdapter2;
    private AddSignOne addSignOne = new AddSignOne();

    private Integer clinchDealType = 1; //签约成交类型  默认第一个
    private int customersId;
    private int type;
    private Integer performanceId;

    @Override
    protected AddSignPresenter createPresenter() {
        return new AddSignPresenter(this);
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_add_sign_one;
    }

    @Override
    public void initView() {
        super.initView();
        setTitleName("签约详情");
        checkBoxAdapter1 = new CheckBoxAdapter(this);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 1);
        /*gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                if (position > 4) {
                    return 1;
                } else {
                    return 2;
                }
            }
        });*/
        recycleView1.setLayoutManager(gridLayoutManager);
        recycleView1.setAdapter(checkBoxAdapter1);


        checkBoxAdapter2 = new CheckBoxAdapter(this);

        recycleView2.setLayoutManager(new GridLayoutManager(this, 3));
        recycleView2.setAdapter(checkBoxAdapter2);


        checkBoxAdapter1.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                checkBoxAdapter1.setSelect(position);
                addSignOne.setPropertyNature(String.valueOf(position + 1));
            }
        });

        checkBoxAdapter2.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                checkBoxAdapter2.setSelect(position);
                addSignOne.setPropertyDecoration(String.valueOf(position + 1));
            }
        });

        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.rb1) {
                    addSignOne.setClinchDealType(1);
                } else if (checkedId == R.id.rb2) {
                    addSignOne.setClinchDealType(2);
                } else {
                    addSignOne.setClinchDealType(3);
                }
            }
        });

    }


    @Override
    public void initData() {
        super.initData();
        type = getIntent().getIntExtra("type", 0);
        customersId = getIntent().getIntExtra("customersId", 0);
        performanceId = getIntent().getIntExtra("performanceId", 0);
        List<String> list1 = new ArrayList<>(); //性质 propertyNature
        list1.add("公寓");
        list1.add("写字楼");
        list1.add("商铺");
        list1.add("工位");
        list1.add("花园");
        list1.add("其他");
        checkBoxAdapter1.setNewData(list1);

        List<String> list2 = new ArrayList<>(); //装修情况 propertyDecoration
        list2.add("全配");
        list2.add("简配");
        list2.add("豪华装修");
        list2.add("简单装修");
        list2.add("毛坯");
        list2.add("其他");
        checkBoxAdapter2.setNewData(list2);
        tvConpany.setText(SPUtils.getInstance(this).getString("company", ""));
        tvStaffName.setText(SPUtils.getInstance(this).getString("name", ""));

       /* String performanceId = SPUtils.getInstance(this).getString("performanceId", "");
        if (performanceId == null || performanceId.equals("")) {
            //
        }*/

        if (type == 1) {  //加号进入
            addSignOne.setPropertyNature("1");
            addSignOne.setPropertyDecoration("1");
            addSignOne.setClinchDealType(1);
        } else {
            mPresenter.getSingnContractdetail(performanceId);
        }

    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    @OnClick({R.id.tv_date, R.id.tv_number_passenger, R.id.tv_search, R.id.tv_ok})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_date:
                showDateDialog();
                break;
            case R.id.tv_number_passenger:
                mPresenter.getPassengerNumber();
                break;
            case R.id.tv_search:
                Intent intent = new Intent(this, AllHousingActivity.class);
                intent.putExtra("type", 2);
                startActivityForResult(intent, 10001);
                break;
            case R.id.tv_ok:
                if (TextUtils.isEmpty(tvDate.getText().toString())) {
                    AppUtils.showToast("请选择日期");
                    return;
                }
                if (TextUtils.isEmpty(tvNumberPassenger.getText().toString())) {
                    AppUtils.showToast("请选择客源");
                    return;
                }
                if (TextUtils.isEmpty(tvNumberHousing.getText().toString())) {
                    AppUtils.showToast("请选择房源");
                    return;
                }
                if (TextUtils.isEmpty(etArea.getText().toString())) {
                    AppUtils.showToast("请输入面积");
                    return;
                }
                addSignOne.setPropertyArea(etArea.getText().toString());
                Log.e("addSignOne", new Gson().toJson(addSignOne));
                mPresenter.saveOneStep(addSignOne);
                break;
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    private void showDateDialog() {
        //获取日历的一个实例，里面包含了当前的年月日
        Calendar calendar = Calendar.getInstance();
        DatePickerDialog dialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                tvDate.setText(year + "-" + month + "-" + dayOfMonth);
                addSignOne.setClosingDate(tvDate.getText().toString() + " 00:00:00");
//                stringBuilderDate.append(year + "-" + month + "-" + dayOfMonth);
            }
        },
                calendar.get(Calendar.YEAR),
                calendar.get(Calendar.MONTH),
                calendar.get(Calendar.DAY_OF_MONTH));
        dialog.show();

    }


    @Override
    public void getPassengerNumberListSuccess(List<PassengerNumberBean> list) {
        List<String> titlts = new ArrayList<>();
        List<Integer> idS = new ArrayList<>();

        for (int i = 0; i < list.size(); i++) {
            titlts.add(list.get(i).getCustomerName() + "-" + list.get(i).getCustomerNumber());
            idS.add(list.get(i).getCustomerId());
        }
        LoopViewPopwindow loopViewPopwindow = new LoopViewPopwindow(this);
        loopViewPopwindow.setIds(idS).setTitleData(titlts).setTitle(" ").showPop(etArea);
        loopViewPopwindow.getView(R.id.tv_sure).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tvNumberPassenger.setText(titlts.get(loopViewPopwindow.position));
                loopViewPopwindow.dismiss();
            }
        });
    }

    @Override
    public void saveOneStepSuccess(BaseData baseData) {

      /*  // 保存performanceId
        JSONObject jsonObject;
        try {
             jsonObject = new JSONObject(resultResponse.data.toString());
            String performanceId1 = jsonObject.getString("performanceId");
            SPUtils.getInstance(this).putString("performanceId", performanceId1);
        } catch (JSONException e) {
            e.printStackTrace();
        }*/

        SaveBean saveBean = new Gson().fromJson(baseData.data.toString(), SaveBean.class);
        ;
        String s = MathUtils.rvZeroAndDot(saveBean.getPerformanceId());
        SPUtils.getInstance(this).putString("performanceId", s);
        //跳转第二部
        Intent intent = new Intent(this, AddSignTwoActivity.class);
        intent.putExtra("type", 2);  //1 加号进入 2  列表进入
        intent.putExtra("customersId", customersId);
        intent.putExtra("performanceId", performanceId);
        startActivity(intent);
    }

    @Override
    public void saveTwoStepSuccess(BaseData baseData) {

    }

    @Override
    public void saveThreeStepSuccess(BaseData baseData) {

    }

    @Override
    public void getSingnContractdetail(SignDetailBean signContractDetailBean) {
        addSignOne.setClinchDealType(signContractDetailBean.getClinchDealType());
        addSignOne.setClosingDate(signContractDetailBean.getClosingDate());
        tvDate.setText(signContractDetailBean.getClosingDate());
        addSignOne.setHousingId(signContractDetailBean.getHousingId());
        tvNumberHousing.setText(signContractDetailBean.getHousingNo());
//        addSignOne.setPropertyConsultant(signContractDetailBean.getPropertyConsultant());
//        etCounselor.setText(signContractDetailBean.getPropertyConsultant());

        tvNumberPassenger.setText(signContractDetailBean.getCustomerNumber());
        addSignOne.setCustomersId(signContractDetailBean.getCustomersId());
        addSignOne.setPropertyNature(String.valueOf(signContractDetailBean.getPropertyNature()));
        checkBoxAdapter1.setSelect(Integer.parseInt(signContractDetailBean.getPropertyNature()) - 1);
        addSignOne.setPropertyDecoration(signContractDetailBean.getPropertyDecoration());
        checkBoxAdapter2.setSelect(Integer.parseInt(signContractDetailBean.getPropertyDecoration()) - 1);
        etArea.setText(signContractDetailBean.getPropertyArea());
        addSignOne.setPropertyArea(signContractDetailBean.getPropertyArea());

    }

    @Override
    public void exChangeSignContractSuccess(BaseData data) {

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 10001) {
            HousesListBean.ListBean listBean = (HousesListBean.ListBean) data.getSerializableExtra("data");
            tvNumberHousing.setText(listBean.getAreaName() + "-" + listBean.getHousingNo());
            addSignOne.setCustomersId(customersId);
            addSignOne.setHousingId(listBean.getHousingId());
        }
    }
}